######################################################################################
#
# This file was provided as part of the SharePoint Mystery Deployment package
# Created by Christopher Kimbell (http://sharepointmystery.com)
#
######################################################################################

# -----------------------------------------------------------------
# Creates an object used to manipulate the Power Point Service
# -----------------------------------------------------------------
function New-PowerPointService()
{
    $obj=new-object PSObject -Property @{
      Name = "PowerPoint Service";
      Proxy = "PowerPoint Service Proxy";
      Pool = New-ApplicationPool;
      Service = New-SupportingService;
      CachePath = "";
      AddToDefaultGroup = $true;
      BroadcastStateIdleTimeoutInSeconds = 5400;
      BroadcastStateSessionTimeoutInSeconds = 43200;
      DisableBinaryScan = $true;
      EditingWorkerProcessCount = 100;
      EditingWorkerTimeoutInSeconds = 60;
      #EnableSandboxedEditing = $true; #prompts for IIS reset, removed so script doesn't stop
      #EnableSandboxedViewing = $true;
      EnableViewing97To2003Formats = $true;
      EnableViewingOpenXmlFormats = $true;
      ViewingCacheExpirationPeriodInSeconds = 600;
      ViewingMaximumConversionsPerWorker = 5;
      ViewingWorkerKeepAliveTimeoutInSeconds = 120;
      ViewingWorkerProcessCount = 3;
      ViewingWorkerTimeoutInSeconds = 300;
    }
    $obj.Service.Name = "PowerPoint Service"
    # -----------------------------------------------------------------
    # Validates the information in the object
    # -----------------------------------------------------------------
    Add-Method $obj "Validate" `
    {
        Write-Operation "Validating Power Point Service"
        $this.Pool.Validate()
        $this.Service.Validate()
        Validate-NotEmpty "Name" $this.Name
        Validate-NotEmpty "Proxy" $this.Proxy 
        Validate-NotEmpty "CachePath" $this.CachePath 
    }
    # -----------------------------------------------------------------
    # Provisions the Service
    # -----------------------------------------------------------------
    Add-Method $obj "Provision" `
    {
        $sa = Get-SPServiceApplication  | Where-Object {$_.DisplayName -eq $this.Name}

        if($sa -eq $null)
        {
            Write-Operation "Creating $($this.Name)"
            $pool = $this.Pool.Provision()
            
            $sa = New-SPPowerPointServiceApplication -Name $this.Name `
                    -ApplicationPool $pool `
                    -CachePath $this.CachePath `
                    -Confirm:$false
        }
        else
        {
            Write-OperationDone "$($this.Name) has already been provisioned"  
        } 
         
        $proxy = Get-SPServiceApplicationProxy  | Where-Object {$_.DisplayName -eq $this.Proxy}  
        if($proxy -eq $null)
        {
            New-SPPowerPointServiceApplicationProxy -Name $this.Proxy `
                -ServiceApplication $sa `
                -AddToDefaultGroup:$this.AddToDefaultGroup 
        }
        else
        {
            Write-OperationDone "$($this.Proxy) has already been provisioned"  
        }
        
        Set-SPPowerPointServiceApplication -Identity $sa `
            -BroadcastStateIdleTimeoutInSeconds $this.BroadcastStateIdleTimeoutInSeconds `
            -BroadcastStateSessionTimeoutInSeconds $this.BroadcastStateSessionTimeoutInSeconds `
            -DisableBinaryScan $this.DisableBinaryScan `
            -EditingWorkerProcessCount $this.EditingWorkerProcessCount `
            -EditingWorkerTimeoutInSeconds $this.EditingWorkerTimeoutInSeconds `
            -EnableViewing97To2003Formats $this.EnableViewing97To2003Formats `
            -EnableViewingOpenXmlFormats $this.EnableViewingOpenXmlFormats `
            -ViewingCacheExpirationPeriodInSeconds $this.ViewingCacheExpirationPeriodInSeconds `
            -ViewingMaximumConversionsPerWorker $this.ViewingMaximumConversionsPerWorker `
            -ViewingWorkerKeepAliveTimeoutInSeconds $this.ViewingWorkerKeepAliveTimeoutInSeconds `
            -ViewingWorkerProcessCount $this.ViewingWorkerProcessCount `
            -ViewingWorkerTimeoutInSeconds $this.ViewingWorkerTimeoutInSeconds 
        
        $this.Service.Provision()
        return $sa
    }
    # -----------------------------------------------------------------
    # Unprovisions the Service
    # -----------------------------------------------------------------
    Add-Method $obj "Unprovision" `
    {
        Remove-ServiceApplicationProxy $this.Proxy
        Remove-ServiceApplication $this.Name
        $this.Service.Unprovision()
    }
    return $obj
}
